import { createRouter, createWebHistory } from 'vue-router'
import Loginpage from '../views/Loginpage.vue'

import { useStore } from '@/stores/counter'
import nprogress from 'nprogress'
import 'nprogress/nprogress.css'
const  mainBox =()=>import('../components/Manage/main/mainBox.vue') 
const router = createRouter({

  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      component: ()=>import('@/views/home.vue'),
      children:[
        {
          path:'home',
          component:()=>import('@/components/Home/home.vue')
        },
        {
          path:'news',
          component:()=>import('@/components/Home/News/news.vue')
        },
        {
          path:'product',
          component:()=>import('@/components/Home/Product/product.vue')
        },
        {
          path:'newpage',
          name:'newpage',
          component:()=>import('@/components/Home/News/newPage.vue')
        }
      ]
    },
    {
      path:'/login',
      name:'login',
      component:()=>import('../views/Loginpage.vue')
    },
  {
    path:'/mainBox',
    name:'mainBox',
    component:mainBox,

    children:[
      {
        path:'home',
        name:'home',
        component:()=>import('../components/Manage/home/home.vue')
      },
      {
        path:'usercenter',
        name:'usercenter',
        component:()=>import('../components/Manage/usercenter/usercenterMain.vue'),
     
      },
      {
        path:'addUser',
        name:'addUser',
        component:()=>import('../components/Manage/userManage/addUser.vue'),
        beforeEnter (to, from, next) {
          const Store =useStore()
          if(Store.NowLoginuser.type===1){
            next()
          }
        }
      },
      {
        path:'userList',
        name:'userList',
        component:()=>import('../components/Manage/userManage/userList.vue'),
        beforeEnter: (to, from, next) => {
          const Store =useStore()
          if(Store.NowLoginuser.type===1){
            next()
          }
        }
      },
      {
        path:'Newadd',
        name:'Newadd',
        component:()=>import('@/components/Manage/New-manager/Newadd.vue')
      },
      {
        path:'NewList',
        name:'NewList',
        component:()=>import('@/components/Manage/New-manager/NewList.vue')
      },
      {
        path:'Newedit',
        name:'newedit',
        component:()=>import('@/components/Manage/New-manager/NewEdit.vue')
      },
      {
        path:'Productadd',
        name:'Productadd',
        component:()=>import('@/components/Manage/Product-manager/Productadd.vue')
      },
      {
        path:'ProductList',
        name:'ProductList',
        component:()=>import('@/components/Manage/Product-manager/ProductList.vue')
      },
      {
        path:'ProductEdit',
        name:'ProductEdit',
        component:()=>import('@/components/Manage/Product-manager/ProductEdit.vue')
      }
    ]
  },


  ]
})
router.beforeEach((to,from,next)=>{
  nprogress.start();
  if(to.path==='/'){
    next('/home')
  }
  if(to.path==='/mainBox')
    next('/mainBox/home')
  else{
    next()
  }
})
router.afterEach((to, from) => {
  nprogress.done();
})
export default router
